<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习五：反转一个数组中的内容</title>
  </head>

  <body>

  </body>
  <script>
    /*
    练习五：反转一个数组中的内容 
     */

    var arr = ['孙悟空', '猪八戒', '沙和尚', '唐僧'];
    //解法一：新数组+push
    /* var newArr = [];
    for (let index = arr.length - 1; index >= 0; index--) {
      newArr.push(arr[index]);
    }
    console.log(newArr); //["唐僧", "沙和尚", "猪八戒", "孙悟空"] */


    //解法二：reverse()
    // arr.reverse();
    // console.log(arr)//["唐僧", "沙和尚", "猪八戒", "孙悟空"]

    //解法三：原数组上做变换
    var temp;
    for (var i = 0; i < arr.length / 2; i++) {
      temp = arr[i];
      arr[i] = arr[arr.length - 1 - i];
      arr[arr.length - 1 - i] = temp;
    }
    console.log(arr); //["唐僧", "沙和尚", "猪八戒", "孙悟空"]
  </script>

</html>